<?php

namespace app\home\controller;

use app\admin\model\Leva;
use app\home\model\Banner;
use app\home\model\CaseModel;
use app\home\model\Config;
use app\home\model\GoodsModel;
use app\home\model\InfoModel;
use app\home\model\NewsModel;
use app\home\model\TypeModel;
use think\Controller;
use think\Db;
use think\Session;
use think\helper;
use think\Request;
use think\Response;
use think\helper\Time;

class Index extends Controller {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub

        if (empty(Session::get('language'))) {
            Session::set('language', 'zh');
        }

        $config = new Config();
        $config_list = $config->getInfo();

        $this->assign('config_list', $config_list);

        $infoModel = new InfoModel();
        $typeModel = new TypeModel();

        // 导航
        $webNavList = $infoModel->getList(15);
        $this->assign('webNavList', $webNavList);

        $caseNavList = $typeModel->getList('case', 13);
        $this->assign('caseNavList', $caseNavList);

        $newsNavList = $typeModel->getList('news', 7);
        $this->assign('newsNavList', $newsNavList);
        // banner大图片
        $bannerList = $this->_banner(5, 105);
        $this->assign('bannerList', $bannerList);
        // 友情
        $linkList = \db()->table('tp_link')
                ->where(['state' => 1])
                ->select();
        $this->assign('linkList1',$linkList);

    }

    public function index() {


        // 案例列表
        $caseModel = new CaseModel();
        $caseList = $caseModel->getListAll();
        $wenda = $caseModel->getList(143);

        //商品列表
//        $goodsModel = new GoodsModel();
//        $goodsList = $goodsModel->getList();
        // 首页底部
        $infoModel = new InfoModel();
        //首页公司简介
        $index_about = $infoModel->getList(145);

        //新闻类型
        $typeModel = new TypeModel();
        $newsTypeList = $typeModel->getTypeList('news', 7);
        $newsTypeId = empty($newsTypeList) ? 0 : $newsTypeList[0]['id'];

        // 新闻
        $newsModel = new NewsModel();
        //
        $newsList1 = $newsModel->getList(101);
        $newsList2 = $newsModel->getList(102);


        // 友情
        $linkList = \db()->table('tp_link')
                ->where(['language' => Session::get('language'), 'state' => 1])
                ->select();

        // banner大图片
        $bannerList = $this->_banner(5, 105);
        // 手机banner大图片
        $bannerList1 = $this->_banner(5, 132);
        // 首页员工风采图片
        $yuangong = $this->_banner(5, 134);
        // 首页 合作 伙伴
        $hezuo = $this->_banner(16, 135);
        return $this->fetch('index', [
                    'bannerList' => $bannerList,
                    'bannerList1' => $bannerList1,
//            'goodsList' => $goodsList,
                    'index_about' => $index_about,
                    'yuangong' => $yuangong,
                    'hezuo' => $hezuo,
                    'newsTypeList' => $newsTypeList,
                    'caseList' => $caseList,
                    'linkList' => $linkList,
                    'wenda' => $wenda,
                    'newsList1' => $newsList1,
                    'newsList2' => $newsList2,
        ]);
    }

    private function _banner($num, $typeId) {
        $model = new Banner();
        return $model->getList($num, ['typeId' => $typeId]);
    }

    private function bannerinfo($id) {
        $model = new Banner();
        return $model->getone($id);
    }

    /*     * 产品中心* */

    public function goods() {
        /*         * 栏目banner图* */
        $pcbanner = $this->bannerinfo(86);
        $sjbanner = $this->bannerinfo(87);
        //获取当前栏目的id
        $id = input('id', 142);
        /*         * 所有商品栏目* */
        $typeModel = new TypeModel();
        $TypeList = $typeModel->getList('goods', 5);
        /*         * 获取当前栏目信息* */
        $Typeinfo = $typeModel->getInfo($id);
        $GoodsModel = new GoodsModel();
        $GoodsList = $GoodsModel->getList($id);


        return $this->fetch('goods', [
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
                    'id' => $id,
                    'typeinfo' => $Typeinfo,
                    'TypeList' => $TypeList,
                    'GoodsList' => $GoodsList,
                        ]
        );
    }

    /*     * 产品详情页* */

    public function goodsInfo($id) {
        /*         * 栏目banner图* */
        $pcbanner = $this->bannerinfo(86);
        $sjbanner = $this->bannerinfo(87);
        /*         * 所有商品栏目* */
        $typeModel = new TypeModel();
        $TypeList = $typeModel->getList('goods', 5);
        /*         * 获取商品信息* */
        $GoodsModel = new GoodsModel();
        $info = $GoodsModel->getInfo($id);
        /*         * 获取商品父栏目信息* */
        $Typeinfo = $typeModel->getInfo($info['typeId']);

        return $this->fetch('goods_info', [
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
                    'typeinfo' => $Typeinfo,
                    'TypeList' => $TypeList,
                    'info' => $info,
        ]);
    }

    /*     * 解决方案* */

    public function cases($id = 0) {

        //获取当前栏目的id
        $id = input('id', 133);
        $caseTypeModel = new TypeModel();
        $typeList = $caseTypeModel->getList('case', 3);
        /*         * 获取当前栏目信息* */
        $Typeinfo = $caseTypeModel->getInfo($id);

        $caseModel = new CaseModel();
        $list = $caseModel->getList($id, 10);
        // 新闻
        $newsModel = new NewsModel();
        //
        $newsList1 = $newsModel->getList(101);
        return $this->fetch('case', [
                    'newsList1' => $newsList1,
                    'id' => $id,
                    'list' => $list,
                    'typeList' => $typeList,
                    'typeinfo' => $Typeinfo,
                    'typeId' => $id,
        ]);
    }

    /*     * 解决方案详情页* */

    public function caseInfo($id) {
        $pcbanner = $this->bannerinfo(88);
        $sjbanner = $this->bannerinfo(89);
        $id = input('id');
        /*         * 所有文章栏目* */
        $typeModel = new TypeModel();
        $typeList = $typeModel->getList('case', 5);

        $caseModel = new CaseModel();
        $info = $caseModel->getInfo($id);
        $pre = $caseModel->getPre($id, $info->typeId);
        $next = $caseModel->getNext($id, $info->typeId);
        /*         * 获取父栏目的信息* */
        $Typeinfo = $typeModel->getInfo($info['typeId']);
        // 新闻
        $newsModel = new NewsModel();
        //
        $newsList1 = $newsModel->getList(101);
        return $this->fetch('case_info', [
                    'newsList1' => $newsList1,
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
                    'typeList' => $typeList,
                    'typeinfo' => $Typeinfo,
                    'info' => $info,
                    'pre' => $pre,
                    'next' => $next,
        ]);
    }

    /*     * 关于我们单页* */

    public function about() {
        if(strpos(request()->url(), 'jjzc')){
            $id = 130;
        }else{
            $id = 129;
        }
        $infoModel = new InfoModel();
//         $typelist = $infoModel->getList(145);
        $info = $infoModel->getOne(['id' => $id]);
        $this->_aboutTypeList(103);


        return $this->fetch('about', [
                    'typeId' => $id,
                    'info' => $info,
        ]);
    }

    private function _aboutTypeList($pid) {
        $typeModel = new TypeModel();
        $TypeList = $typeModel->getListc('info', $pid, 5);
        $this->assign('TypeList', $TypeList);
        return $TypeList;
    }

    /*     * 公司新闻* */

    public function newsList() {
        /*         * 栏目banner图* */
        $pcbanner = $this->bannerinfo(90);
        $sjbanner = $this->bannerinfo(91);

        //获取当前栏目的id
        $id = input('id', 101);
        $TypeModel = new TypeModel();
        $typeList = $TypeModel->getList('news', 4);
        /*         * 获取当前栏目信息* */
        $Typeinfo = $TypeModel->getInfo($id);

        $newsModel = new NewsModel();
        $newsList = $newsModel->getList($id, 6);


        return $this->fetch('news', [
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
                    'typeList' => $typeList,
                    'typeinfo' => $Typeinfo,
                    'id' => $id,
                    'newsList' => $newsList,
        ]);
    }

    private function _newsTypeList() {
        $typeModel = new TypeModel();
        $newsTypeList = $typeModel->getList('news', 5);
        $this->assign('newsTypeList', $newsTypeList);

        return $newsTypeList;
    }

    public function newsInfo() {
        /*         * 栏目banner图* */
        $pcbanner = $this->bannerinfo(90);
        $sjbanner = $this->bannerinfo(91);
        $id = input('id');
        /*         * 所有文章栏目* */
        $typeModel = new TypeModel();
        $TypeList = $typeModel->getList('news', 5);


        $newsModel = new NewsModel();
        $info = $newsModel->getInfo($id);
        /*         * 获取商品父栏目信息* */
        $Typeinfo = $typeModel->getInfo($info['typeId']);

        $pre = $newsModel->getPre($id, $Typeinfo["id"]);
        $next = $newsModel->getNext($id, $Typeinfo["id"]);

        return $this->fetch('news_info', [
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
                    'typeinfo' => $Typeinfo,
                    'TypeList' => $TypeList,
                    'info' => $info,
                    'pre' => $pre,
                    'next' => $next,
        ]);
    }

    /*     * 联系我们页面* */

    public function contact() {
        /*         * 栏目banner图* */
        $pcbanner = $this->bannerinfo(92);
        $sjbanner = $this->bannerinfo(93);
        return $this->fetch('contact', [
                    'pcbanner' => $pcbanner,
                    'sjbanner' => $sjbanner,
        ]);
    }

    public function getContent() {

        $leva = new Leva();
        $leva->data(Request::instance()->param());
        $leva->created = time();

        if ($leva->save() == 0) {
            return (['code' => 403, 'msg' => '留言失败' . $leva->getError()]);
        }
        return (['code' => 200, 'msg' => '留言成功']);
    }

    public function times() {
        $list = Db::table('tp_info')
                        ->where(['language' => Session::get('language'), 'state' => 1, 'typeId' => 22])
                        ->order('sort desc')->select();

        return $this->fetch('times', [
                    'list' => $list,
        ]);
    }

    public function addLeva() {

        $leva = new Leva();
        $leva->data(Request::instance()->param());
        $leva->created = time();

        if ($leva->save() == 0) {
            return (['code' => 403, 'msg' => '留言失败' . $leva->getError()]);
        }
        return (['code' => 200, 'msg' => '留言成功']);
    }

    //合作伙伴
    public function hezuo() {
        $info = $this->_banner(5, 147);

        return $this->fetch('hezuo', [
                    'info' => $info,
        ]);
    }

    //公司厂房
    public function lianxi() {
        $id = input('id', 131);
        $infoModel = new InfoModel();
//         $typelist = $infoModel->getList(146);

        $info = $infoModel->getOne(['id' => $id]);
        $this->_aboutTypeList(103);

        $typelist = array(
            array(
                'name' => $info['name'],
                'id' => null
            )
        );

        return $this->fetch('lianxi', [
                    'typelist' => $typelist,
                    'typeId' => $id,
                    'info' => $info,
        ]);
    }
       //精英招聘
    public function jingying() {
        $id = input('id', 132);
        $infoModel = new InfoModel();
        $typelist = $infoModel->getList(149);
        $info = $infoModel->getOne(['id' => $id]);
        $this->_aboutTypeList(149);

        return $this->fetch('jingying', [
                    'typelist' => $typelist,
                    'typeId' => $id,
                    'info' => $info,
        ]);
    }

    public function sitemap($type = null){
        $this->view->engine->layout(false);
        $domain = config('url_domain_root');
        $server_name = $domain ? trim($domain, '/') : trim(input('server.SERVER_NAME'), '/');
        $request_scheme = input('server.REQUEST_SCHEME');
        $prefix = ($request_scheme ? $request_scheme : 'http').'://'.$server_name;

        $sitemap = array();
        $tpl = 'sitemap';
        if(in_array($type, array('cases', 'news', 'hezuo', 'other'))){

            switch ($type){
                case 'cases':
                    $caseModel = new CaseModel();
                    $list = $caseModel->getList(133, 1000);
                    if(! empty($list)){
                        foreach ($list as $item){
                            $sitemap[] = array(
                                'url' => url('@caseInfo', array('id' => $item['id']), 'html', true),
                                'date' => date('Y-m-d', $item['created'])
                            );
                        }
                    }
                break;
                case 'news':
                    $newsModel = new NewsModel();
                    $list = $newsModel->getList(101, 1000);
                    if(! empty($list)){
                        foreach ($list as $item){
                            $sitemap[] = array(
                                'url' => url('@newsInfo', array('id' => $item['id']), 'html', true),
                                'date' => date('Y-m-d', $item['created'])
                            );
                        }
                    }
                break;
                default:
                    $date = date('Y-m-d', Time::today()[0]);
                    $suffix = 'html';
                    $sitemap = array(
                        array(
                            'url' => $prefix.'/cases.'.$suffix,
                            'date' => $date
                        ),
                        array(
                            'url' => $prefix.'/news.'.$suffix,
                            'date' => $date
                        ),
                        array(
                            'url' => $prefix.'/hezuo.'.$suffix,
                            'date' => $date
                        ),
                        array(
                            'url' => $prefix.'/lianxi.'.$suffix,
                            'date' => $date
                        ),
                        array(
                            'url' => $prefix.'/about.'.$suffix,
                            'date' => $date
                        ),
                        array(
                            'url' => $prefix.'/jjzc.'.$suffix,
                            'date' => $date
                        ),
                    );
            }
            $tpl = 'sitemap_type';
        }else{
            $suffix = 'xml';
            $date = date('Y-m-d', Time::today()[0]);
            $sitemap = array(
                array(
                    'url' => $prefix.'/sitemap/type/other.'.$suffix,
                    'date' => $date
                ),
                array(
                    'url' => $prefix.'/sitemap/type/cases.'.$suffix,
                    'date' => $date
                ),
                array(
                    'url' => $prefix.'/sitemap/type/news.'.$suffix,
                    'date' => $date
                ),
            );
        }

        $xml = $this->fetch($tpl, [
            'sitemap' => $sitemap
        ]);
        $response = new Response();
        return $response->contentType('text/xml')->content($xml);
    }

}
